package com.icangya.interceptor;

import com.icangya.Utils.JSONUtils;
import com.icangya.Value.FootBall;
import com.icangya.domain.User;
import com.icangya.service.IMoneyService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by 王小红 on 2017/5/16.
 */
public class ReleaseInterceptor extends HandlerInterceptorAdapter {

    private Logger logger = Logger.getLogger(this.getClass());

    //@Autowired
   // IUserDAO iUserDAO;


    @Autowired
    IMoneyService iMoneyService;

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {
        /** 拦截到用户的请求了 */

        //用户未充值不能查看广告
        //用户每天12点之后未充值不能查看广告
        User user =  (User) request.getSession().getAttribute(FootBall.FOOT_USER_SESSION);
        boolean bool=iMoneyService.getRechargeRecord(user.getOpenId());
//        boolean bool=iMoneyService.getRechargeRecord("oxn-ruOzhsoIxfGd8-vdp-JBeZWs");
        if(bool) {
            return true;
        }
        return false;

//        String ua = ((HttpServletRequest) request).getHeader("user-agenter").toLowerCase();
//        if (ua.indexOf("micromessenger") > 0) {// 是微信浏览器
//            return  true;
//        }else{
//            response.sendRedirect("https://baike.baidu.com/item/%E7%A5%9E%E7%BB%8F%E7%97%85");
//            return false;
//        }

//        String requestType = request.getHeader("X-Requested-With");
//        logger.info(requestType);

//        if (requestType==null){
//            response.sendRedirect("https://baike.baidu.com/item/%E7%A5%9E%E7%BB%8F%E7%97%85");
//            return false;
//        }else{
//            logger.info(requestType);
//            return true;
//        }


    }


    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
    }


}
